
package com.example.demo.service.impl;

import com.example.demo.entity.*;
import com.example.demo.controller.vo.CourseDetailVO;
import com.example.demo.entity.CoursePO;
import com.example.demo.entity.OrderPO;
import com.example.demo.entity.UserCourse;
import com.example.demo.entity.UserOrder;
import com.example.demo.mapper.CourserMapper;
import com.example.demo.mapper.UserCourseMapper;
import com.example.demo.service.CourseService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {

    @Resource
    private CourserMapper courserMapper;

    @Autowired
    private UserCourseMapper userCourseMapper;

    @Override
    public CoursePO getCourseDetail(long id) {
        CoursePO coursePO = courserMapper.selectCourseDetail(id);
        return coursePO;
    }

    @Override
    public List<CoursePO> getCourseListByUserId(String id, Integer key) {
        List<Long> list = userCourseMapper.selectCourseListByUserId(id);
        List<CoursePO> coursePOS = new ArrayList<>();
        list.forEach(courseId ->{
            CoursePO coursePO = courserMapper.selectCourseDetail(courseId);
            coursePOS.add(coursePO);
        });
        return sort(coursePOS,key);
    }

    @Override
    public List<CoursePO> getCourseListByTuijian(Integer key) {
        List<CoursePO> list = courserMapper.selectCourseList();
        return sort(list,key);
    }



    private List<CoursePO> sort(List<CoursePO> list,Integer key){
        if(key == 2){
            Comparator<CoursePO> courseDetailVOComparator = Comparator.comparingLong(CoursePO::getSaleNums);
            Collections.sort(list,courseDetailVOComparator);
        }else if(key == 3){
            Comparator<CoursePO> courseDetailVOComparator = Comparator.comparing(CoursePO::getRealCost);
            Collections.sort(list,courseDetailVOComparator);
        }
        return list;
    }


    @Override
    public List<CoursePO> getMarkCourseListByTitle(String title) {
        return courserMapper.selectCourseListByTitle(title);
    }
//    @Override
//    public List<CoursePO> getMarkCourseListByTitle(int type,String title) {
//        return courserMapper.selectCourseListByTitle(type,title);
//    }




    //*********************************************//
    public List<CoursePO> getCourseList(String userId) {
        List<UserCourse> courseUser =  userCourseMapper.selectCourseList(userId);
        List<CoursePO> res = new ArrayList<>();
        for(UserCourse userCourse : courseUser){
            res.add(courserMapper.selectCourseByCourseId(userCourse.getCourseId()));
        }
        return res;
    }



//    @Override
//    public List<OrderPO> getOrderList(String userId) {
//        List<UserOrder> userOrders =  userOrderMapper.selectOrderList1(userId);
//        List<OrderPO> res = new ArrayList<>();
//        for(UserOrder userOrder : userOrders){
//            res.add(orderMapper.selectOrderByOrderId(userOrder.getOrderId()));
//        }
//        return res;
//    }
}
